package com.tianquan.mybatis;/**
 * @author starsea
 * @date 2024-09-11 上午10:56
 */

import com.alibaba.druid.pool.DruidDataSource;
import com.tianquan.mapper.DishMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.mybatis
 * @Author: tianquan
 * @CreateTime: 2024-09-11  10:56
 * @Description: 测试
 * @Version: 1.0
 */

// @Component
public class MybatisConfig {

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/sky_take_out?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setInitialSize(5);
        return dataSource;
    }

    @Bean
    public Configuration configuration(DataSource dataSource) {
        Configuration configuration = new Configuration();
        JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("1", jdbcTransactionFactory, dataSource);
        configuration.setEnvironment(environment);
        configuration.setCacheEnabled(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        configuration.addMapper(DishMapper.class);
        return configuration;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(Configuration configuration) {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        return sqlSessionFactory;
    }
//
}
